package edu.jd.authoritymanagement;

import edu.jd.log.Log;
import edu.jd.log.LogService;
import edu.jd.login.CurrentUser;

import java.util.Date;
import java.util.List;

public class AuthorityController {
    private static AuthorityController authorityController = new AuthorityController();
    private AuthorityService authorityService = new AuthorityService();
    private LogService logService = new LogService();

    private AuthorityController(){}

    public static AuthorityController get(){
        return authorityController;
    }

    public boolean checkAuthority(Authority requireAuthority){
        boolean ans = false;
        if(CurrentUser.getCurrentUser()==null){
            return false;
        }else{
            List<Integer> roleList = authorityService.getRoleIdByAuthId(requireAuthority.getAuth_id());
            for(int role_id : roleList){
                if(role_id == CurrentUser.getCurrentUser().getRole_id()){
                    ans = true;
                }
            }
            Log log = new Log(CurrentUser.getCurrentUser().getUser_id(),
                    requireAuthority.getAuth_id(),
                    CurrentUser.getCurrentUser().getRole_id(),
                    new Date(),
                    ans);
            logService.writeLog(log);
        }
        return ans;
    }
}
